capture log close
clear all
set maxvar 10000
set more off
pause off
# delimit;

local crime "";
local output "";

/* 
Table B3 
estimates using rdrobust
*/


local exp "prwora";
local demo_list "all male female" ; 

local vars "n_crime2char_18_38 n_crime2genr_18_38 n_crime2nogn_18_38 n_crime2ahinc_18_38 avg_kidemp15_18_38
			n_crime2chart_18_38 n_crime2genrt_18_38 n_crime2nognt_18_38 n_crime2shinc_18_38
			" ; 
	
use "`crime'\prep2\crime_prwora_prepforreg_exactage.dta", clear ;

drop if dobdd_run == . ;

capture drop *noemp* ;
capture drop *crimeno* ;
capture drop *week* ;
capture drop *wknd* ;
capture drop *traf* ;
capture drop n_crime*emp* ;
capture drop n_crime*prob* ;
capture drop n_crime*par* ;
capture drop n_crime*vlp* ;

/* Subsamples */
gen demo_all = 1 ;
gen byte demo_othment = (n_diag1_mental == 1 & (substr(dibdig,1,3) != "317" & substr(dibdig,1,3) != "318" & substr(dibdig,1,3) != "319" )) ;
gen byte demo_nonment = (demo_othment == 0) ;
gen byte demo_twopar = (n_fam_singmom != 1 & n_fam_nopar != 1) ;
gen byte demo_singno = (n_fam_singmom == 1) | (n_fam_nopar ==1) ;
gen byte demo_nopar  = (n_fam_nopar == 1) ;
gen byte demo_male 	 = (n_male ==1) ;
gen byte demo_female = (n_male !=1) ;

gen postXdobdd=n_post*dobdd_run;

/* Covariates */
foreach var in n_firstage n_earnhhbins { ;
	levelsof `var', local(l_`var') ;
	foreach l of local l_`var' { ;
		gen byte `var'_`l' = (`var' == `l') ;
	} ;
} ;

foreach st in 
	ar az ca co ct fl ks il in md mi mn ms nc nd ne nj oh or pa tx wa wi va
	{ ;
	gen byte state_`st' = (state == "`st'") ;
	} ;
	
gen byte n_gender_male = (n_male == 1) ;
gen byte n_gender_fem  = (n_male == 0) ;
	
local full_cov_all "n_gender_ n_dibmdr_ n_fam_ n_diag1_ n_firstage_ n_earnhhbins_ state_" ;
local full_cov_singno "n_gender_ n_dibmdr_ n_fam_ n_diag1_ n_firstage_ n_earnhhbins_ state_" ;
local full_cov_othmen "n_gender_ n_dibmdr_ n_fam_ n_diag1_ n_firstage_ n_earnhhbins_ state_" ;
local full_cov_nonment "n_gender_ n_dibmdr_ n_fam_ n_diag1_ n_firstage_ n_earnhhbins_ state_" ;
local full_cov_twopar "n_gender_ n_dibmdr_ n_diag1_ n_firstage_ n_earnhhbins_ state_" ;
local full_cov_male "n_dibmdr_ n_fam_ n_diag1_ n_firstage_ n_earnhhbins_ state_" ;
local full_cov_female "n_dibmdr_ n_fam_ n_diag1_ n_firstage_ n_earnhhbins_ state_" ;


global cutoff = 0 ;
global rd_parameters  "all bwselect(msecomb2) p(1) q(2) kernel(uni)" ;

foreach time in wk mo { ;
	bysort dob`time'_run: gen no`time' = _n ;
} ;

foreach demo in `demo_list' { ;

	preserve ;
	keep if demo_`demo' == 1 ;
	matrix prwora_robust_`demo' 	= J(1000,14,.) ;
	local count = 0 ;
	local matrix_list = "" ;

	foreach var of varlist `vars' { ;

		display "***** `var' *****" ;
	
			summ `var' ;
			if r(mean) > 0.008 { ;
		
			local matrix_list "`matrix_list' `var'" ;
			
			foreach group in `full_cov_`demo'' { ;
				local `group'c "" ;
				foreach cov of varlist `group'* { ;
					qui summ `cov' if `var' != . ;
					if r(mean) > 0.01 { ;
						local `group'c "``group'c' `cov'" ;
					} ;
 				} ;
				di "``group'c'" ;
				pause ;
			} ;
			
			local `var'c "" ;
			foreach group in `full_cov_`demo'' { ;
				local ct_`group': word count ``group'c' ;
				forval i = 1(1)`=`ct_`group''-1' { ;
					local `var'c "``var'c' `: word `i' of ``group'c''" ;
				} ;
			} ;
			di "``var'c'" ;
			pause ;
			
			/* Prep for reg */
			local count = `count' + 1 ;
				  
			foreach time in wk mo { ;
				cap drop `var'`time' ;
				bys dob`time'_run: egen `var'`time' = mean(`var') ;
				bys dob`time'_run: egen N_`var'`time' = count(`var') ;
				} ;

			/* Defaulting lines to be off screen in case we can run the fuzzy model */
			local rd_bw_l = ${cutoff} - 99999 ;
			local rd_bw_r = ${cutoff} - 99999 ;  
			local iv_bw_l = ${cutoff} - 99999 ;  
			local iv_bw_r = ${cutoff} - 99999 ; 

			rdrobust `var' dobdd_run, c(${cutoff}) $rd_parameters vce(hc2) covs(``var'c') ;
				local rd_bw_l = ${cutoff} - e(h_l) ;
				local rd_bw_r = ${cutoff} + e(h_r) ;
				
				matrix prwora_robust_`demo'[`count',1] = e(tau_cl) ;
				matrix prwora_robust_`demo'[`count',2] = e(se_tau_cl) ;
				matrix prwora_robust_`demo'[`count',3] = e(tau_bc) ;
				matrix prwora_robust_`demo'[`count',4] = e(se_tau_rb) ;
				matrix prwora_robust_`demo'[`count',5] = e(N) ;
				matrix prwora_robust_`demo'[`count',6] = e(N_h_l) ;
				matrix prwora_robust_`demo'[`count',7] = e(N_h_r) ;		
			
			if "`var'" != "n_age18unfav" {  ;
			
				rdrobust `var' dobdd_run, c(${cutoff}) $rd_parameters vce(hc2) covs(``var'c') fuzzy(n_age18unfav) ;
					local iv_bw_l = ${cutoff} - e(h_l) ;
					local iv_bw_r = ${cutoff} + e(h_r) ;
					
					matrix prwora_robust_`demo'[`count',8]  = e(tau_cl) ;
					matrix prwora_robust_`demo'[`count',9]  = e(se_tau_cl) ;
					matrix prwora_robust_`demo'[`count',10] = e(tau_bc) ;
					matrix prwora_robust_`demo'[`count',11] = e(se_tau_rb) ;
					matrix prwora_robust_`demo'[`count',12] = e(N) ;
					matrix prwora_robust_`demo'[`count',13] = e(N_h_l) ;
					matrix prwora_robust_`demo'[`count',14] = e(N_h_r) ;		
				
			} ; /* end if */
			
		} ; /* end if */   
	} ;	/* end var */

	/* Save estimates and SEs */	
	clear ;
	svmat prwora_robust_`demo' ; 
	rename (prwora_robust_`demo'*) (rd_b_reg rd_se_reg rd_b_bc rd_se_bc rd_N rd_N_h_l rd_N_h_r iv_b_reg iv_se_reg iv_b_bc iv_se_bc iv_N iv_N_h_l iv_N_h_r) ;

	gen var = "" ;
	local count = 0 ;
	foreach var in `matrix_list' { ;
		local count = `count' + 1 ;
		replace var = "`var'" if _n == `count' ;
	} ;
	order var ;
	save "`output'//`exp'_robust_`demo'_exactage.dta", replace ;
	restore ;
} ; /* end demo */

			
capture log close;
